package com.hxzy.common.util;

import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONUtil;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.common.vo.R;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.Enumeration;
import java.util.Map;

/**
 * 获取Servlet(Spring方式)
 */
public class ServletUtils {

    public static ServletRequestAttributes getRequestAttributes(){
        try
        {
            RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
            return (ServletRequestAttributes) attributes;
        }
        catch (Exception e)
        {
            return null;
        }
    }


    /**
     * 获取request
     */
    public static HttpServletRequest getRequest()
    {
        try
        {
            return getRequestAttributes().getRequest();
        }
        catch (Exception e)
        {
            return null;
        }
    }

    /**
     * 获取response
     */
    public static HttpServletResponse getResponse()
    {
        try
        {
            return getRequestAttributes().getResponse();
        }
        catch (Exception e)
        {
            return null;
        }
    }


    /**
     * 根据键获取请求中的值
     * @param key
     * @return
     */
    public static String getParams(String key){
        return getRequest().getParameter(key);
    }

    /**
     *  得到参数把变整数
     * @param key
     * @return
     */
    public static Integer getParamsToInt(String key){
        String value=getParams(key);
        if(NumberUtil.isInteger(value)){
            return  Integer.parseInt(value);
        }
        else{
            throw new ServiceException(AckCode.SYSTEM_PARAM_FAIL);
        }
    }

    /**
     * 得到参数把变整数
     * @param key
     * @param defaultValue  默认值
     * @return
     */
    public static Integer getParamsToInt(String key,int defaultValue){
        String value=getParams(key);
        if(NumberUtil.isInteger(value)){
            return  Integer.parseInt(value);
        }
        else{
            return defaultValue;
        }
    }

    /**
     * 向前端输出json数据
     * @param response
     * @param jsonValue
     */
    public static void renderString(HttpServletResponse response,String jsonValue){
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        Writer writer = null;
        try {
            writer = response.getWriter();
            writer.write(jsonValue);
            writer.flush();
        } catch (IOException e) {
            throw new UtilException(e);
        } finally {
            IoUtil.close(writer);
        }
    }

    /**
     * 向前端输出json数据
     * @param response
     * @param r
     */
    public static void renderString(HttpServletResponse response, R r){
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        Writer writer = null;
        try {
            writer = response.getWriter();
            writer.write(JSONUtil.toJsonStr(r));
            writer.flush();
        } catch (IOException e) {
            throw new UtilException(e);
        } finally {
            IoUtil.close(writer);
        }
    }


    /**
     * 获取到本次请求头
     * @param request
     * @return
     */
    public static Map<String, String> getHeaders(HttpServletRequest request){
        Map<String, String> map = new LinkedCaseInsensitiveMap<>();
        Enumeration<String> enumeration = request.getHeaderNames();
        if (enumeration != null)
        {
            while (enumeration.hasMoreElements())
            {
                String key = enumeration.nextElement();
                String value = request.getHeader(key);
                map.put(key, value);
            }
        }
        return map;
    }
}
